package com.cx.miaodi;

import net.sf.json.JSONObject;

import java.net.URLEncoder;

public class GetMessageCode {
    private static String accountSid = Config.ACCOUNT_SID;
    private static String rod = smsCode();//生成一个随机验证码

    //生成验证码
    public static String smsCode(){
        String random = (int)((Math.random()*9+1)*100000)+"";
        return random;
    }

    //根据相应的手机号发送验证码
    public static String getCode(String phone) throws Exception{
        StringBuilder sb = new StringBuilder();
        sb.append("accountSid").append("=").append(accountSid);
        sb.append("&to").append("=").append(phone);
        sb.append("&param").append("=").append(URLEncoder.encode(rod));
        sb.append("&templateid").append("=").append("233907");
//		sb.append("&smsContent").append("=").append(smsContent);
        String body = sb.toString() + HttpUtil.createCommonParam(Config.ACCOUNT_SID, Config.AUTH_TOKEN);
        //提交请求
        String result = HttpUtil.post(Config.BASE_URL, body);
        System.out.println(result);

        //字符串转json对象
        JSONObject jsonObject = JSONObject.fromObject(result);
        String respCode = jsonObject.getString("respCode");

        System.out.println("验证码："+rod);
        System.out.println("结果码："+respCode);
        //反馈-00000状态码标识请求成功，
        String defaultRespCode="0000";
        return rod;

    }
}
